package com.bang.sgg.listener;

import com.bang.sgg.persist.model.AgentSeat;
import com.bang.sgg.persist.model.constant.QueueConstant;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.amqp.core.Message;

import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @Auther: Bang
 * @Date: 2019/11/23 11:23
 * @Description:
 */
@Component
public class RabbitmqConsumer {

    @RabbitListener(queues = QueueConstant.AGENT_HELLO)
    @RabbitHandler
    public void helloQueue(Message message, Channel channel, @Payload AgentSeat agentSeat) {
        System.err.println("---------------helloQueue-----------------------");
        System.err.println("消费端Payload: " );
        System.err.println(agentSeat);
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        //手工ACK
        try {
            channel.basicAck(deliveryTag, false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @RabbitListener(queues = QueueConstant.AGENT_WORLD)
    @RabbitHandler
    public void worldQueue(Message message, Channel channel, @Payload AgentSeat agentSeat) {
        System.err.println("---------------worldQueue-----------------------");
        System.err.println("消费端Payload: " );
        System.err.println(agentSeat);
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        //手工ACK
        try {
            channel.basicAck(deliveryTag, false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @RabbitListener(queues = QueueConstant.AGENT_HANGUP)
    @RabbitHandler
    public void hangUp(Message message, Channel channel, @Payload AgentSeat agentSeat) throws IOException {
        System.err.println("---------------hangUp-----------------------");
        System.err.println("消费端Payload: " );
        System.err.println(agentSeat);
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        // 手工ACK
        try {
            // deliveryTag是消息传送的次数，我这里是为了让消息队列的第一个消息到达的时候抛出异常
            // ，处理异常让消息重新回到队列，然后再次抛出异常，处理异常拒绝让消息重回队列
            if (deliveryTag == 1 || deliveryTag == 2) {
                throw new Exception("不消费");
            }

            System.err.println("消费消息...............");
            // false只确认当前一个消息收到，true确认所有consumer获得的消息
            channel.basicAck(deliveryTag, false);
        } catch (Exception e) {
            System.err.println(e.getMessage());
            if (message.getMessageProperties().getRedelivered()) {
                System.out.println("消息已重复处理失败,拒绝再次接收...");
                try {
                    channel.basicReject(deliveryTag, false); // 拒绝消息
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            } else {
                System.out.println("消息即将再次返回队列处理...");
                channel.basicNack(deliveryTag, false, true); // requeue为是否重新回到队列
            }
        }

    }


    @RabbitListener(queues = QueueConstant.FANOUT_QUEUE1)
    @RabbitHandler
    public void fanoutQueue1(Message message, Channel channel, @Payload AgentSeat agentSeat) {
        System.err.println("---------------fanoutQueue1-----------------------");
        System.err.println("消费端Payload: " );
        System.err.println(agentSeat);
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        //手工ACK
        try {
            channel.basicAck(deliveryTag, false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RabbitListener(queues = QueueConstant.FANOUT_QUEUE2)
    @RabbitHandler
    public void fanoutQueue2(Message message, Channel channel, @Payload AgentSeat agentSeat) {
        System.err.println("---------------fanoutQueue2-----------------------");
        System.err.println("消费端Payload: " );
        System.err.println(agentSeat);
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        //手工ACK
        try {
            channel.basicAck(deliveryTag, false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
